package com.gen.generator.actions;

import com.gen.generator.config.CodeGenConfig;
import com.gen.generator.service.CodeGenerator;
import com.gen.generator.ui.ConfigDialog;
import com.gen.generator.util.ErrorUtil;
import com.gen.generator.util.YamlUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;

import javax.swing.*;
import java.awt.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;

public class GenerateCodeAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        ConfigDialog dialog = new ConfigDialog(null);
        dialog.pack(); // 或 dialog.setSize(width, height)
        dialog.setLocationRelativeTo(null); // 居中在屏幕
        dialog.setVisible(true);

        if (!dialog.isConfirmed()) return; // 用户取消

        try {
            List<Map<String, String>> templates = YamlUtil.loadTemplates();
            CodeGenConfig config = dialog.getConfig(templates);
            if (config == null) return;

            CodeGenerator.generate(config);

            JOptionPane.showMessageDialog(null, "代码生成成功！");
        } catch (Exception ex) {
            ErrorUtil.showError(ex,null);
        }
    }
}
